<?php
	class categoryControl extends publicControl{
		function cate_show($by){
			C("debug",1);
			$this->head();
			$cid=isset($_GET['cateid'])?$_GET['cateid']:'';
			$by=isset($_GET['by'])?$_GET['by']:'time';
			$cate=$this->channel($cid);//获得子栏目
			// p($cate);

			$str=$this->xuqiu_show($cid);//获得栏目及子栏目ID
			$xuqiu=$this->get_xuqiu($str,$by);//规则排序
			$this->assign("cate",$cate);
			$xuqiu['cid']=$cid;
			$this->assign("xuqiu",$xuqiu);
			$this->display();
	}
		function channel($id){/*******列出所有的子栏目********/
		$db=M('category');
		$row=$db->where("cateid='$id'")->find();
		switch ($row['level']) {
			case 0:
				# code...
			$cid=$row['cateid'];
			$child=array();
			$count=$db->where("pid='$cid'")->count();
			if($count%2==0){
				$num=$count/2;
			}else{
				$num=($count/2)+0.5;
			}
			$bor=array();
			$bor["0"]=$db->where("pid='$cid'")->limit('0',$num)->all();
			$bor["1"]=$db->where("pid='$cid'")->limit($num,$count)->all();
			if(empty($bor)){
				return;
			}
			foreach ($bor as $k => $v) {
				 foreach($v as $m =>$n){
				$cid=$n['cateid'];
				$child=$db->where("pid='$cid'")->all();
				
				$bor[$k][$m]['child']=$child;
			}
				# code...
			}

			return $bor;
				break;
			case 1:
			$cid=$row['cateid'];
			$bor['0']=$db->where("pid='$cid'")->all();
			$bor['parent']=$db->where("cateid='$cid'")->find();
			return $bor;
			default:
				# code...
				break;
			case 2:
			$pid=$row['pid'];
			$bor['0']=$db->where("pid='$pid'")->all();
			$bor['parent']=$db->where("cateid='$pid'")->find();
			return $bor;
		}

	}

	function xuqiu_show($cid){
		$db=M("category");
		$row=$db->where("cateid='$cid'")->find();
		$str='';
		switch($row['level']){
			case '0'://当等级为0的时候 获得他下面所有的栏目中的需求
				$cid=$row['cateid'];
				$child=$db->field("cateid")->where("pid='$cid'")->all();
				foreach ($child as $k => $v) {//先获得子栏目中的CID
					$str.=implode(",",$v).",";
				}	
				foreach($child as $k=> $v){//获得子栏目下面的子栏目的CID
					$cateid=$v['cateid'];
					$childs[$k]=$db->field("cateid")->where("pid='$cateid'")->all();
					foreach ($childs[$k] as $m => $n) {
						$str.=implode(",",$n).",";
					}
				}
				$str=substr($str, 0,-1);//剔除掉最后一个逗号
				$str="(".$str.")";
				
			return $str;
				break;
			case 1:
				$cid=$row['cateid'];	
				$child=$db->field("cateid")->where("pid='$cid'")->all();
				foreach ($child as $v) {
					$str.=implode(",",$v).",";
					# code...
				}
			$str=substr($str,0,-1);
			$str="(".$str.")";
			return $str;
			case 2:
			$str="(".$row['cateid'].")";

			return $str;

		}



	}


	function get_xuqiu($str,$by){
		$db=K("xuqiu");

		if($str!==''){
		$rows=$db->where("xuqiu.xid in $str")->order("xuqiu."."$by"." desc")->all();
		return $rows;
		}
		else{
		$rows=$db->where("xuqiu.xid =1")->order("xuqiu."."$by"." desc")->all();
		return $rows;
		}
	}




	}










?>